package com.my.house.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.my.house.common.ApiAuth;
import com.my.house.common.utils.ThreadLocalHolder;
import com.my.house.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;

/**
 * @author yangll
 */
@Controller()
@RequestMapping("page")
public class HtmlController {

    @ApiAuth(anonymous = true)
    @GetMapping("login.html")
    public String login() {
        return "login";
    }

    @ApiAuth(anonymous = true)
    @GetMapping("register.html")
    public String register() {
        return "register";
    }

    @GetMapping("main.html")
    public String userMain(Model model) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        model.addAttribute("userName", DesensitizedUtil.chineseName(user.getUserName()));
        model.addAttribute("mobile", DesensitizedUtil.mobilePhone(user.getMobile()));
        Integer rentDay = user.getRentDay();
        LocalDate now = LocalDate.now();
        LocalDate date = LocalDate.of(now.getYear(), now.getMonth(), rentDay);
        if (now.isAfter(date)) {
            // 收租日已过
            model.addAttribute("start", date);
            model.addAttribute("end", now);
        } else {
            model.addAttribute("start", now);
            model.addAttribute("end", date);
        }
        return "main";
    }

    @GetMapping("settings.html")
    public String settings() {
        return "settings";
    }
}
